package com.lh.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lh.entity.table.ApprovalRecord;
import com.lh.entity.table.RectificationInfo;
import com.lh.entity.vo.ApprovalRecordVo;
import com.lh.mapper.ApprovalRecordMapper;
import com.lh.mapper.RectificationInfoMapper;
import com.lh.service.ApprovalRecordService;
import com.lh.service.RectificationInfoService;
import com.lh.utils.Answer;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class ApprovalRecordServiceImpl implements ApprovalRecordService {
    @Resource
    private ApprovalRecordMapper approvalRecordMapper;

    @Resource
    private RectificationInfoService rectificationInfoService;

    @Resource
    private RectificationInfoMapper rectificationInfoMapper;

    @Override
    public Answer<Object> addApprovalRecord(ApprovalRecord approvalRecord) {
        approvalRecord.setApprovalTime(new Date());
        approvalRecordMapper.insert(approvalRecord);
        return Answer.ok("审批记录成功");
    }

    @Override
    public Answer<Object> getApprovalRecordPage(JSONObject reqJson) {
        Integer pageSize = reqJson.getInteger("pageSize");
        Integer pageNum = reqJson.getInteger("pageNum");
        IPage<ApprovalRecord> page = new Page<>(pageNum, pageSize);
        String title = reqJson.getString("title");
//        String projectType = reqJson.getString("projectType");
//        String projectChildType = reqJson.getString("projectChildType");
//        String projectChildCategoryType = reqJson.getString("projectChildCategoryType");
        RectificationInfo rectificationInfo = new RectificationInfo();
        if (StringUtils.isNotBlank(title)) {
            rectificationInfo.setTitle(title);
        }

        IPage<ApprovalRecord> approvalRecordIPage = approvalRecordMapper.selectPage(page, new LambdaQueryWrapper<>());
        List<ApprovalRecord> records = approvalRecordIPage.getRecords();
        ApprovalRecordVo approvalRecordVo = new ApprovalRecordVo();
        List<ApprovalRecordVo> approvalRecordVoList = new ArrayList<>();

        Answer<Object> objectAnswer = rectificationInfoService.queryRectificationList(rectificationInfo);
        List<RectificationInfo> result = (List<RectificationInfo>)objectAnswer.getResult();
        Map<Integer,RectificationInfo> map = new HashMap<>();
        if (result!=null&&!result.isEmpty()){
            map = result.stream().collect(Collectors.toMap(RectificationInfo::getId,x->x));
        }
        for (ApprovalRecord record : records){
            RectificationInfo rectificationInfo1 = map.get(record.getRectificationId());
            BeanUtils.copyProperties(record,approvalRecordVo);
            approvalRecordVo.setTitle(rectificationInfo1.getTitle());
            approvalRecordVo.setCurrentNode(rectificationInfo1.getCurrentNode());
            approvalRecordVo.setLabel(rectificationInfo1.getLabel());
            approvalRecordVo.setLeadUnit(rectificationInfo1.getLeadUnit());
            approvalRecordVo.setPersonLiaible(rectificationInfo1.getPersonLiaible());
            approvalRecordVo.setLeadLeader(rectificationInfo1.getLeadLeader());
            approvalRecordVoList.add(approvalRecordVo);
        }
        IPage<ApprovalRecordVo> approvalRecordVoIPage = new Page<>();
        approvalRecordVoIPage.setRecords(approvalRecordVoList);
        approvalRecordVoIPage.setTotal(approvalRecordIPage.getTotal());
        approvalRecordVoIPage.setSize(approvalRecordIPage.getSize());
        return Answer.ok(approvalRecordVoIPage);
    }

    @Override
    public Answer<Object> titleList() {
        List<ApprovalRecord> approvalRecords = approvalRecordMapper.selectList(new LambdaQueryWrapper<>());
        List<RectificationInfo> rectificationInfoList = rectificationInfoMapper.selectList(new LambdaQueryWrapper<>());
        Map<Integer,RectificationInfo> map = new HashMap<>();
        if (rectificationInfoList!=null&&!rectificationInfoList.isEmpty()){
            map = rectificationInfoList.stream().collect(Collectors.toMap(RectificationInfo::getId,x->x));
        }
        List<String> titleList = new ArrayList<>();
        for (ApprovalRecord approvalRecord:approvalRecords){
            RectificationInfo rectificationInfo = map.get(approvalRecord.getRectificationId());
            if (rectificationInfo!=null&&rectificationInfo.getTitle()!=null)
            titleList.add(rectificationInfo.getTitle());
        }
        return Answer.ok(titleList);
    }

}
